package com.yinhe.music.yhmusic.db.dao;

import com.hwangjr.rxbus.RxBus;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.yhmusic.db.AbstractBaseDb;
import com.yinhe.music.yhmusic.db.DbHelper;
import com.yinhe.music.yhmusic.db.bean.MenuDBEntity;
import com.yinhe.music.yhmusic.log.MLog;

import java.util.List;


/**
 * Created by wangyang on 17/3/18.
 */

public class MenuEntityDAO extends AbstractBaseDb {

    private static MenuEntityDAO mInstance = null;
    private Dao dao;
    private static final String TAG = MenuEntityDAO.class.getSimpleName();

    public MenuEntityDAO(DbHelper dbHelper) {
        super(dbHelper);
        dao = getDao(MenuDBEntity.class);
    }

    public boolean insertMenu(final MenuDBEntity menuDBEntity) {
        try {
            dao.createIfNotExists(menuDBEntity);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean insertMenuList(final List<MenuDBEntity> menuDBEntities) {
        try {
            for (MenuDBEntity i : menuDBEntities) {
                dao.createOrUpdate(i);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean updateMenu(final MenuDBEntity menuDBEntity) {
        try {
            dao.update(menuDBEntity);
            RxBus.get().post(RxBusEventType.SongMenu.UPDATE_MENU,menuDBEntity);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    public boolean updateMenuList(final List<MenuDBEntity> menuDBEntities) {
        try {
            for (MenuDBEntity i : menuDBEntities) {
                dao.update(i);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<MenuDBEntity> getAllMenuSyn() {
        try {
            List<MenuDBEntity> list = (List<MenuDBEntity>) dao.queryForAll();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public boolean isExistIdSyn(final int menuId) {
        try {
            MenuDBEntity info = (MenuDBEntity) dao.queryForId(menuId);
            if (info != null) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public MenuDBEntity getMenuByIdSyn(final int menuId) {
        try {
            return (MenuDBEntity) dao.queryForId(menuId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<MenuDBEntity> getMenuByEditableSyn(final int editable) {
        try {
            MLog.debug(TAG, "get success");
            return (List<MenuDBEntity>) dao.queryForEq(MenuDBEntity.MenuColums.EDITABLE, editable);
        } catch (Exception e) {
            e.printStackTrace();
            MLog.debug(TAG, "ERROR:" + e.toString());
            return null;
        }
    }


    public long getMenuCount() {
        try {
            return dao.queryBuilder().countOf();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }


    public void deleteMenuListByIdSyn(List<Integer> ids) {
        try {
            DeleteBuilder<MenuDBEntity, Integer> deleteBuilder = dao.deleteBuilder();
            deleteBuilder.where().in(MenuDBEntity.MenuColums.MENU_ID, ids);
            deleteBuilder.delete();
            MLog.debug(TAG, "delete " + ids.size());
        } catch (Exception e) {
            MLog.debug(TAG, "delete fail:" + e.toString());
            e.printStackTrace();
        }
    }

    public void deleteMenuByIdSyn(int id) {
        try {
            DeleteBuilder<MenuDBEntity, Integer> deleteBuilder = dao.deleteBuilder();
            deleteBuilder.where().eq(MenuDBEntity.MenuColums.MENU_ID, id);
            deleteBuilder.delete();
            MLog.debug(TAG, "delete " + id);
        } catch (Exception e) {
            MLog.debug(TAG, "delete fail:" + e.toString());
            e.printStackTrace();
        }
    }
}
